/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package spatialmarketplace.cem.dao.implementation;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sourceforge.stripes.util.Log;
import spatialmarketplace.cem.dao.DBHandler;
import spatialmarketplace.cem.dao.UserDao;
import spatialmarketplace.cem.model.User;

/**
 *
 * @author Camila
 */
public class UserDaoImpl implements UserDao{
    
    private static final Log logger = Log.getInstance(ForumPostDaoImpl.class);
    private static UserDaoImpl instance = new UserDaoImpl();

    public static UserDaoImpl getInstance() {
        return instance;
    }


    @Override
    public ArrayList<User> readUsers() {
        DBHandler dbInstance = DBHandler.getInstance();
        ArrayList<User> userList=new ArrayList<User>();
        User usr = null;
        Integer userId;
        String userName;
        String photo;
        String profileDescription;
        String position;
        String contactNumber;
        String email;
        
        try{
           if(dbInstance!=null){
              String query = new String("SELECT * FROM SM_User");
              dbInstance.openConnection();  
              ResultSet rs = dbInstance.executeQuery(query); 
                            
              try 
                {                    
                    while (rs.next()) {
                        userId = rs.getInt("userId");
                        userName = rs.getString("userName");
                        photo = rs.getString("photo");
                        profileDescription = rs.getString("profileDescription");
                        position = rs.getString("position");
                        contactNumber = rs.getString("contactNumber");
                        email = rs.getString("email");

                        usr = new User(userId, userName, photo, profileDescription, position, contactNumber, email);
                       
                        
                        //add to the user list
                        userList.add(usr);
                    }  
         
                    dbInstance.closeConnection();

                }catch (SQLException ex) {
                    Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex);
                }
                    
           }
               
               
       }catch(Exception e){
              System.out.println("Error: "+e);
              logger.error("ERROR: "+GroupDaoImpl.class.getName()+", "+ e);
       }
         
        
        return userList;
    }

    @Override
    public User readUser(Integer userId) {
        
        DBHandler dbInstance = DBHandler.getInstance();
        User usr = null;
        
        String userName;
        String photo;
        String profileDescription;
        String position;
        String contactNumber;
        String email;

        try {
            if (dbInstance != null) {
                String query = new String("SELECT * FROM SM_User WHERE userId = " + userId);

                dbInstance.openConnection();
                ResultSet rs = dbInstance.executeQuery(query);

                try {
                    while (rs.next()) {
                        userName = rs.getString("userName");
                        photo = rs.getString("photo");
                        profileDescription = rs.getString("profileDescription");
                        position = rs.getString("position");
                        contactNumber = rs.getString("contactNumber");
                        email = rs.getString("email");

                        usr = new User(userId, userName, photo, profileDescription, position, contactNumber, email);
                        
                    }

                    dbInstance.closeConnection();

                } catch (SQLException ex) {
                    ex.printStackTrace();
                    Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error: " + e);
            logger.error("ERROR: " + ForumPostDaoImpl.class.getName() + ", " + e);
        }
        return usr;
        
    }

    @Override
    public void updateUser(Integer userId, User user) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
